
const express = require('express')
const app = express();           //新建App
var path=require('path');
const cors = require('cors');
app.use(cors({
origin: 'http://localhost:3000',
methods:['GET','POST'],
alloweHeaders:['Conten-Type', 'Authorization'],
credentials: true    // 是否带cookie
}));

// POST数据的解析需要有中间件先解析一层
// body.urlencoded会给req加上一个body属性
// req.body本来就是不存在的
// 加上之后后面才可以正常输出
var bodyParser = require('body-parser');

app.use(bodyParser.json({limit: '5mb'}));    //最大上传大小不超过50mb

app.use(bodyParser.urlencoded({

 limit: '50mb',

 extended:true

}));

app.use(express.static(path.join(__dirname, 'dist')));

const registerRouter = require('./subModules/register')
app.use('/register', registerRouter)

const loginRouter=require('./subModules/login')
app.use('/login', loginRouter)

const galleryRouter = require('./subModules/gallery')
app.use('/gallery', galleryRouter)

const detailRouter=require('./subModules/detail')
app.use('/detail', detailRouter)

const commentRouter = require('./subModules/addNewComment')
app.use('/addcomment', commentRouter)
const getCommentsRouter = require('./subModules/getMoreComments')
app.use('/getcomments', getCommentsRouter)

const postRouter=require('./subModules/addNewPost')
app.use('/addpost', postRouter)

const searchRouter = require('./subModules/search')
app.use('/search', searchRouter)

const userHomeRouter=require('./subModules/userHome')
app.use('/userhome', userHomeRouter)

app.get('/', function (req, res) { //请求，响应
 console.log(req);
res.send('<h1>服务器已经起动了，get请求</h1>')
})
app.post('/', function (req, res) { //请求，响应
var data=JSON.parse(req.query[0])
console.log(data);
res.send('<h1>服务器已经起动了，post请求</h1>')
})

app.listen(8888, function () {  //监听9093端口
console.log('node app start at port 8888')
})



//app.use(express.static(path.join(__dirname, 'dist')));






